<template>
  <el-dialog :visible.sync="dialog"
             :title="$t('custom.component.gadget.selectWidget')"
             @closed="clearFormData"
             class="apps-tabs-layout"
             width="620px"
             append-to-body>
    <div class="select_widget_filter_condition">
      <el-row>
        <el-col :span="18">
          <div class="filter_condition_lf">
            <div class="filter_condition">{{$t('custom.component.gadget.filter')}}</div>
            <div>
              <el-input :placeholder="$t('custom.component.gadget.search')"
                        @input="checkedName"
                        v-model="searchVal">
                <i slot="prefix"
                   class="el-input__icon el-icon-search"></i>
              </el-input>
            </div>
          </div>
        </el-col>
        <el-col :span="6"> </el-col>
      </el-row>
    </div>
    <div class="widget_data_list"
         v-loading="widgetLoading">
      <el-checkbox-group v-model="checkedCities"
                         @change="handleCheckedCitiesChange">
        <el-checkbox v-for="(val,index) in  businessTypeListrecord.slice((currentPage-1)*5,currentPage*5)"
                     :label="val.gadgetId"
                     :key="index"
                     :class="'checked'?'el-checkted':''">
          <div class="item_widget">
            <img style="width:40px;height:40px;margin-right:20px" :src="gadgetTypeImgUrl(val.gadgetType)">
            <div class="item_widget_content">
              <div>{{val.name}}</div>
              <div>{{val.description}}</div>
            </div>
          </div>
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div>
      <el-pagination layout="prev, pager, next"
                     :total="businessTypeListrecord.length"
                     :page-size="5"
                     :page-count='currentPage'
                     :current-page='currentPage'
                     @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <template slot="footer">
      <el-button size="small"
                 @click="dialog = false">{{ $t('Cancel') }}</el-button>
      <el-button type="primary"
                 size="small"
                 @click="addGadgetToPageGroup()">{{ $t('Add') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { dashboardGadgetList } from '@/utils/data-json'
export default {
  props: {
    gadgets: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      dialog: false,
      searchVal: '',
      currentPage: 1,
      dataForm: {
        dataset: ''
      },
      businessTypeListrecord: [],
      checkedCities: [],
      widgetLoading: false,
      userGadgetList: []
    }
  },
  watch: {
    userGadgetList (val) {
      this.widgetLoading = false
      this.businessTypeListrecord = this.filterHasWidget(val)
    },
    dialog (newVal) {
      if (newVal) {
        this.getSaveGadgetList()
        this.widgetLoading = true
      }
    },
    '$store.state.bot.routeMeta': function (val) {
      this.getDashboardLayout()
    }
  },
  computed: {
    GadgetList: {
      get () {
        let tempList = []
        tempList = this.userGadgetList
        return tempList
      },
      set (newVal) {
        this.businessTypeListrecord = newVal
      }
    },
    businessTypeList () {
      return window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_type_list').dataList
    }
  },
  methods: {
    gadgetTypeImgUrl (type) {
      let obj = this._.filter(dashboardGadgetList, { key: type })
      return obj[0].imageUrl || ''
    },
    openDialog () {
      this.dialog = true
    },
    clearFormData () {
      this.currentPage = 1
      this.searchVal = ''
      this.dataForm.dataset = ''
      this.businessTypeListrecord = []
      this.checkedCities = []
    },
    checkedName (val) {
      this.currentPage = 1
      if (this.businessTypeForWidget === 'Template') {
        this.GadgetList = this.filterHasWidget(this.userGadgetList).filter(e => e.businessType === 'Template').filter(e => e.name.toLowerCase().indexOf(val.toLowerCase()) !== -1)
      } else {
        this.GadgetList = this.filterHasWidget(this.userGadgetList).filter(e => e.name.toLowerCase().indexOf(val.toLowerCase()) !== -1)
      }
    },
    handleCheckedCitiesChange (value) {
      this.checkedGauge = value
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage
    },
    addGadgetToPageGroup () {
      this.$emit('addWidgetToPage', this.getAddWidgetList(this.checkedGauge))
      this.dialog = false
      this.checkedGauge = []
    },
    getAddWidgetList (selectList) {
      let tempList = []
      selectList.map(selectGadgetId => {
        if (this.gadgets.findIndex(e => e.i === selectGadgetId) > -1) {
          this.$message.error(this.$t('custom.report.disabledAddSaveGadget'))
        }
        let item = this.businessTypeListrecord.find(e => e.gadgetId === selectGadgetId)
        let newWidget = {
          gadgetId: item.gadgetId,
          gadgetType: item.gadgetType
        }
        tempList.push(newWidget)
      })
      return tempList
    },
    filterHasWidget (tempList) {
      if (this.businessTypeForWidget !== 'MSPbots Internal') {
        if (this.businessTypeForWidget === 'Template') {
          tempList = this._.reject(tempList, { businessType: 'Custom' })
        }
        tempList = this._.reject(tempList, { businessType: 'MSPbots Internal' })
      }
      let arr = []
      let tempListArr = []

      this.gadgets.forEach(element => {
        arr.push(element.i)
      })
      tempList.forEach(element => {
        if (arr.indexOf(element.gadgetId) === -1) {
          tempListArr.push(element)
        }
      })
      return tempListArr
    },
    getSaveGadgetList () {
      const params = {
        reuse: true,
        tenantCode: this.$store.state.user.tenantCode,
        showAllIntegration: true,
        internal: this.businessTypeForWidget === 'MSPbots Internal',
        current: 1,
        queryType: 'REFRESHCACHE',
        size: 999999,
        filter: {}
      }
      this.$interface.reports.getReportDataNewCardListApi(params)
        .then(({
          data: res
        }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.userGadgetList = res.data.records
        })
        .catch(() => { })
    }
  }
}
</script>

<style lang="scss">
.apps-tabs-layout {
  .select_widget_filter_condition {
    background: url("../../../assets/select_widget-filter-icon.png");
    background-repeat: no-repeat;
    background-position: center left;
    background-size: 14px 14px;
    padding-left: 20px;
    .filter_condition_lf {
      .filter_condition {
        margin-top: 8px;
        margin-left: 5px;
      }
    }
    .filter_condition_lf > div {
      float: left;
      width: 150px;
      margin-right: 10px;
    }
    .filter_condition_lf > div:nth-child(1) {
      width: auto;
    }
  }
  .select_widget_filter_condition::after {
    content: ".";
    clear: both;
    display: block;
    overflow: hidden;
    height: 0;
  }
  .widget_data_list {
    margin-top: 20px;
    min-height: 20px;
    .el-checkbox-group {
      > .is-checked {
        border: 1px solid #138dc7;
      }
    }
    .el-checkbox {
      display: block;
      margin-right: 0;
      background: #ffffff;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      margin-top: 10px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .el-checkbox__input {
        display: block;
        width: 54px;
        text-align: center;
      }
      .el-checkbox__label {
        flex: 1;
        display: block;
        height: 100%;
        padding-left: 0;
        .item_widget {
          // background: url("../../../assets/grid.png");
          background-size: 48px 48px;
          background-repeat: no-repeat;
          background-position: left center;
          height: 100%;
          // padding-left: 54px;
          padding-top: 5px;
          padding-bottom: 5px;
          display: flex;
          justify-content: space-around;
          align-items: center;
          min-height: 70px;
          .item_widget_content {
            flex: 1;
            height: 100%;
            padding-right: 10px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            > div {
              white-space: pre-wrap;
              word-break: break-word;
            }

            > div:nth-child(1) {
              color: #000;
              font-size: 14px;
            }
            > div:nth-child(2) {
              font-size: 14px;
              color: rgba(0, 0, 0, 0.49);
            }
          }
          .item_widget_ri {
            float: right;
            width: 53px;
            height: 100%;
            text-align: center;
            .item {
              height: 14px;
              width: 14px;
              border: 0;
              background: url("../../../assets/select_widget_eye.png");
              background-size: 14px 14px;
              background-repeat: no-repeat;
              background-position: center;
            }
          }
          position: relative;
        }
        .item_widget::after {
          content: "";
          clear: both;
          display: block;
          overflow: hidden;
          position: absolute;
          right: 53px;
          border-right: 1px solid #f0f0f0;
          height: 80%;
        }
      }
    }
    .el-checkbox::after {
      content: ".";
      clear: both;
      display: block;
      overflow: hidden;
      height: 0;
    }
  }
  .el-pagination {
    text-align: center;
  }
  .layoutTitle {
    font-size: 12px;
    word-break: keep-all;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .layoutTitle:hover {
    overflow: visible;
  }

  .vue-grid-item.vue-grid-placeholder {
    background: #f2f8ff;
    border: 1px solid #38f;
    opacity: 1;
  }

  .layoutJSON {
    background: #ddd;
    border: 1px solid black;
    margin-top: 10px;
    padding: 10px;
  }

  .eventsJSON {
    background: #ddd;
    border: 1px solid black;
    margin-top: 10px;
    padding: 10px;
    height: 100px;
    overflow-y: scroll;
  }

  .columns {
    -moz-columns: 120px;
    -webkit-columns: 120px;
    columns: 120px;
  }

  .vue-resizable-handle {
    z-index: 5000;
    position: absolute;
    width: 20px;
    height: 20px;
    bottom: 0;
    right: 0;
    background: url("");
    background-position: bottom right;
    padding: 0 3px 3px 0;
    background-repeat: no-repeat;
    background-origin: content-box;
    box-sizing: border-box;
    cursor: se-resize;
  }

  .editmode:not(.vue-grid-placeholder) {
    border: 1px solid #38f;
    opacity: 0.5;
  }

  .vue-grid-item.resizing {
    opacity: 0.9;
  }

  .vue-grid-item .no-drag {
    height: 100%;
    width: 100%;
  }

  .vue-grid-item .minMax {
    font-size: 12px;
  }

  .vue-grid-item .add {
    cursor: pointer;
  }

  .vue-draggable-handle {
    position: absolute;
    width: 20px;
    height: 20px;
    top: 0;
    left: 0;
    background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><circle cx='5' cy='5' r='5' fill='#999999'/></svg>")
      no-repeat;
    background-position: bottom right;
    padding: 0 8px 8px 0;
    background-repeat: no-repeat;
    background-origin: content-box;
    box-sizing: border-box;
    cursor: pointer;
  }

  .layout-item {
    text-align: center;
    margin-bottom: 15px;
    padding: 0 5px;
  }

  .layout-item .bottom {
    position: relative;
    margin-top: 15px;
  }

  .layout-item .bottom span {
    display: block;
    float: left;
    width: 50%;
    margin-top: 12px;
    text-align: left;
  }

  .layout-item .bottom button {
    padding-top: 0;
  }

  .top-toolbar {
    position: relative;
    margin-top: -15px;
    height: 35px;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    background-color: #fff;
    margin-left: -15px;
    padding-top: 10px;
    width: calc(100% + 30px);
  }

  .top-toolbar .top-toolbar-title {
    padding-left: 25px;
  }

  .btnBox {
    height: 16px;
    padding: 0px 10px;
    background: #c4ebf3;
    position: absolute;
    top: -15px;
    right: 24px;
    border-bottom-left-radius: 6px;
    border-bottom-right-radius: 6px;
  }

  .showBtn {
    font-size: 18px;
    color: #00c4ed;
  }

  .closeBtn {
    font-size: 18px;
    position: absolute;
    top: 8px;
    right: 11px;
  }
}
</style>
